<?php
/**
 * TinyCatPHP 极简、快速、创新、易用、高性能的PHP应用开发框架
 * User: CnTaiGe <hst042@163.com>
 * Date: 2017/10/31
 */

namespace TinyCatPHP\exception;

use TinyCatPHP\libs\config\Config;
use TinyCatPHP\libs\handler\Interceptor;
use TinyCatPHP\libs\handler\Response;
use TinyCatPHP\libs\utils\Log;
use TinyCatPHP\libs\utils\Running;

/**
 *  系统异常类
 * Class sysException
 * @package TinyCatPHP\exception
 */
class SysException extends \Exception
{
    protected static $errorCode = 0;
    protected $code = -2;

    /**
     * 注册异常错误
     */
    public static function register()
    {
        error_reporting(E_ALL);
        register_shutdown_function([__CLASS__, 'shutdown']);
        set_exception_handler([__CLASS__, 'exception']);
        set_error_handler([__CLASS__, 'error']);
    }

    public static function shutdown()
    {
        Interceptor::instance()->endApp();
        Log::record('info', '整个应用执行完成 ' . '[总运行时间: ' . Running::tagTime('app_run') . 's] ' . '[总使用内存: ' . Running::getMemory() . ']');
        Log::save();
    }

    public static function exception($ex)
    {
        if ($ex->getCode() === -1) {
            Response::responseCode(404);
            Log::record('error', $ex->getMessage() . ' request_content: ' . print_r($_SERVER, true));
            self::errorHtml($ex->getMessage(), '');
        } elseif ($ex->getCode() === -2) {
            Response::responseCode(500);
            Log::record('error', $ex->getMessage() . ' request_content: ' . print_r($_SERVER, true));
            self::errorHtml($ex->getMessage(), '');
        } else {
            Response::responseCode(500);
            Log::record('error', $ex->getMessage() . 'file: ' . $ex->getFile() . ' line：' . $ex->getLine());
            self::errorHtml($ex->getMessage(), 'file: ' . $ex->getFile() . ' line：' . $ex->getLine());
        }
    }

    public static function error($errno, $errstr, $errfile, $errline)
    {
        if (self::$errorCode === $errno)
            return;
        self::$errorCode = $errno;
        Response::responseCode(500);
        Log::record('error', 'message: ' . $errstr . ' file: ' . $errfile . ' line: ' . $errline . ' request_content: ' . print_r($_SERVER, true));
        self::errorHtml($errstr, 'file: ' . $errfile . ' line: ' . $errline);
    }

    public static function errorHtml($errorMsg, $tips)
    {
        Response::endOut();
        if (!Config::get('debug') && (error_reporting() > 1)) return;
        if (!Config::get('debug') && http_response_code() !== 404) {
            $errorMsg = '众里寻他千百度。蓦然回首，那服务器却开了个小差。';
            $tips = '';
        }
        $html = "<!DOCTYPE html>
                    <html lang=\"en\">
                    <head>
                    <meta charset=\"UTF-8\">
                    <title>Error-TinyCatPHP</title>
                    </head>
                    <body>
                    <h2>{$errorMsg}</h2>
                    <h3>$tips</h3>
                    </body>
                    </html>";
        echo $html;
        if (!Config::get('debug') && http_response_code() !== 404) {
            exit();
        }
    }
}